package com.danciyixue.boot.dcyx.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.danciyixue.boot.dcyx.mapper.SeriesMapper;
import com.danciyixue.boot.dcyx.service.SeriesService;
import com.danciyixue.boot.dcyx.model.entity.Series;
import com.danciyixue.boot.dcyx.model.form.SeriesForm;
import com.danciyixue.boot.dcyx.model.query.SeriesQuery;
import com.danciyixue.boot.dcyx.model.vo.SeriesVO;
import com.danciyixue.boot.dcyx.converter.SeriesConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 系列信息服务实现类
 *
 * @author youlaitech
 * @since 2025-06-15 16:29
 */
@Service
@DS("dcyx")
@RequiredArgsConstructor
public class SeriesServiceImpl extends ServiceImpl<SeriesMapper, Series> implements SeriesService {

    private final SeriesConverter seriesConverter;

    /**
    * 获取系列信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SeriesVO>} 系列信息分页列表
    */
    @Override
    public IPage<SeriesVO> getSeriesPage(SeriesQuery queryParams) {
        Page<SeriesVO> pageVO = this.baseMapper.getSeriesPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取系列信息表单数据
     *
     * @param id 系列信息ID
     * @return 系列信息表单数据
     */
    @Override
    public SeriesForm getSeriesFormData(Long id) {
        Series entity = this.getById(id);
        return seriesConverter.toForm(entity);
    }
    
    /**
     * 新增系列信息
     *
     * @param formData 系列信息表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveSeries(SeriesForm formData) {
        Series entity = seriesConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新系列信息
     *
     * @param id   系列信息ID
     * @param formData 系列信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateSeries(Long id,SeriesForm formData) {
        Series entity = seriesConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除系列信息
     *
     * @param ids 系列信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteSeriess(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的系列信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
